package jspSamples.unit7.websiteSample;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Function {
    DBConnection DBConnection = new DBConnection();

    /* 数据库查询*/
    public Boolean CheckLogin(Connection conn, String user, String pwd) throws SQLException {
        boolean OK = true;
        // 1.创建Statement
        Statement statement = conn.createStatement();
        // 2. sql
        String sql = "select * from admin where AdminName='" + user + "'" + "and AdminPwd='" + pwd + "'";


        // 3. executeQuery
        ResultSet resultSet = statement.executeQuery(sql);
        if (!resultSet.next()) {
            OK = false;
        } else {
            OK = true;
        }
        return OK;
    }


    public String Page(String sPage, ResultSet rs, int intPage, int intPageSize) {
        // intPageSize 每页显示条目数 intPage 当前页码 sPage 跳转页码
        // 获取总共条目数
        try {
            // rs.last() 是让指针移动到最后，获取一共条目数
            rs.last();
            int intRowCount = rs.getRow(); //h获取条目数
            int intPageCount;// 总共页数
            if (intRowCount % intPageSize == 0) {
                // 如果整除，一公分几页
                intPageCount = intRowCount / intPageSize;
            } else {
                // 不整除，多一页
                intPageCount = intRowCount / intPageSize + 1;
            }
            if (intPageCount == 0) {
                intPageCount = 1;
            } else {

            }
            // Math.floor() 向下取整  ————比如3.5 向下取整为3
            int PageCount = (int) Math.floor(intRowCount / intPageSize) + 1; // 一共可以分为几页

        } catch (SQLException e) {
            e.printStackTrace();
        }
        String s = null;

        return "";
    }

    public StringBuffer ListNews(StringBuffer sb, ResultSet rs) throws SQLException {
        if (!rs.next()) {
            sb.append("<div>暂无数据....</div>");
        } else {
            while (rs.next()) {
                sb.append("<tr>");
                sb.append("<tr>" + rs.getString("NewsID") + "</td>");
                sb.append("<tr>" + rs.getString("NewsTile") + "</td>");
                sb.append("<tr>" + rs.getString("NewsContent") + "</td>");
                sb.append("<tr>" + rs.getString("NewsTime") + "</td>");
                sb.append("<tr>" + rs.getString("AdminName") + "</td>");
                sb.append("<td>\n" +
                        "                    <span class=\"btn-sm btn-primary\">修改</span>\n" +
                        "                    <span class=\"btn-sm btn-danger\">删除</span>\n" +
                        "                </td>");


            }

        }
        return sb;
    }
}
